﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBBase
{
    public class DBBsLanguageConfig
    {
        
        EFYihongIotContext ef = new EFYihongIotContext();
        public bs_language_config getLanguagesConfig(string c_guid)
        {
            return ef.bs_language_config.Where(c => c.c_guid == c_guid).FirstOrDefault();
        }

        public List<bs_language_config_item> getLanguagesConfigItems(string c_guid)
        {
            return ef.bs_language_config_item.Where(c => c.ci_c_guid == c_guid).ToList();
        }
        public DataTable getLanguagesConfigItemsForDataTable(string c_guid)
        {
            string sql = "select * from bs_language_config_item inner join bs_language on ci_l_guid=l_guid and ci_c_guid=@c_guid";
            return MySqlYihongFileServer.GetDataTable(sql, new { c_guid });
        }
        private static object obj = new object();
        public bool saveAll(bs_language_config config)
        {
            lock (obj)
            {

                //先查一下有没重复提交
                bs_language_config model = ef.bs_language_config.Where(c => c.c_guid == config.c_guid).FirstOrDefault();
                if (model == null)
                {
                    //查编码
                    bs_language_config getcode = ef.bs_language_config.OrderByDescending(c => c.c_id).FirstOrDefault();
                    if (getcode != null && getcode.c_code != null)
                    {
                        config.c_code = getcode.c_code + 1;
                    }
                    else
                    {
                        config.c_code = 1000;
                    }
                    //写入
                    config.c_addtime = DateTime.Now;
                    ef.bs_language_config.Add(config);
                    ef.SaveChanges();

                }
                else
                {
                    model.c_name = config.c_name;
                    model.c_addtime = DateTime.Now;
                    ef.bs_language_config.Update(model);
                    ef.SaveChanges();
                }
            }
            return true;

        }
        public bool saveItem(bs_language_config_item configItem)
        {

            //先查一下有没重复提交
            bs_language_config_item model = ef.bs_language_config_item.Where(c => c.ci_guid == configItem.ci_guid).FirstOrDefault();
            if (model == null)
            {
                //写入
                configItem.ci_c_code = ef.bs_language_config.Where(c => c.c_guid == configItem.ci_c_guid).FirstOrDefault().c_code;
                configItem.ci_addtime = DateTime.Now;
                ef.bs_language_config_item.Add(configItem);
                ef.SaveChanges();
            }


            return true;

        }


    }

}
